import { ApplyCaseExperience } from "~/api";

export const useContent = () => {
    const route = useRoute();
    const visible = ref(false);
    const phone = ref("");
    const id = route.params.id;

    const onApply = async () => {
        if (isEmpty(phone.value)) {
            Message().warning("请输入联系方式!");
            return false;
        }

        if (!/^[+-\d]{11,}$/.test(phone.value)) {
            Message().warning("联系方式格式有误，请检查!");
            return false;
        }

        const res = await ApplyCaseExperience({
            phone: phone.value,
            examcase_id: id,
        });

        if (res.code == 200) {
            Message().success("申请成功!");
            onClose();
        } else {
            Message().error("网络错误!");
        }
    };

    const onClose = () => {
        visible.value = false;
        phone.value = "";
    };

    return {
        phone,
        visible,
        onClose,
        onApply,
    };
};
